package com.gaoyi.crm.repository;

import com.gaoyi.crm.R;
import com.gaoyi.crm.apiresult.ApiResultData;
import com.gaoyi.crm.apiresult.PagedList;
import com.gaoyi.crm.callback.Callback;
import com.gaoyi.crm.callback.OnResultListener;
import com.gaoyi.crm.constant.ApiStatus;
import com.gaoyi.crm.constant.DbMsg;
import com.gaoyi.crm.db.dao.ItemNoStoreDao;
import com.gaoyi.crm.db.dto.SyncedItemDto;
import com.gaoyi.crm.db.entity.ItemNoStore;
import com.gaoyi.crm.db.entity.ItemNoStoreWithBean;
import com.gaoyi.crm.db.query.CtmSyncedQuery;
import com.gaoyi.crm.repository.base.BaseRepository;
import com.gaoyi.crm.util.OKHttpUtil;
import com.gaoyi.crm.util.StringUtil;
import com.gaoyi.crm.util.ThreadManager;
import com.gaoyi.crm.util.Utils;

import java.lang.reflect.Type;
import java.util.Date;
import java.util.List;

public class ItemNoStoreRepository extends BaseRepository<ItemNoStore, ItemNoStoreDao> {
    @Override
    protected int getIndex() {
        return 2;
    }

    @Override
    protected ItemNoStoreDao getDao() {
        return mDatabase.getItemNoStoreDao();
    }

    public void insertItemNoStore(List<ItemNoStore> itemNoStores) {
        mDao.inserts(itemNoStores);
    }

    public void getItemNo(String itemNo, OnResultListener onResultListener, Type typeofT) {
        if (Utils.isOnline()) {
            getItemNoOnLine(itemNo, onResultListener, typeofT);
        } else {
            getItemNo(itemNo, onResultListener);
        }
    }

    private void getItemNo(String itemNo, OnResultListener onResultListener) {
        ThreadManager.getInstance().execute(() -> {
            ApiResultData<ItemNoStore> result = new ApiResultData<>();
            ItemNoStoreWithBean bean = mDao.getItemNoStore(itemNo);
            String msg = DbMsg.QUERY_SUCCESS;
            int status = ApiStatus.SUCCESS;
            ItemNoStore itemNoStore = null;
            if (bean != null && bean.itemNoStore != null) {
                bean.itemNoStore.setCategory(bean.category);
                itemNoStore = bean.itemNoStore;
            } else {
                status = ApiStatus.FAIL;
                msg = StringUtil.getString(R.string.no_data);
            }
            result.setStatus(status);
            result.setMessage(msg);
            result.setData(itemNoStore);
            OKHttpUtil.getInstance().post(() -> onResultListener.onSuccess(result));
        });
    }

    private void getItemNoOnLine(String itemNo, OnResultListener onResultListener, Type typeofT) {
        OKHttpUtil.getInstance().getAsync(OKHttpUtil.NET_URL + "/api/Syns/client/scanItemNo?itemNo=" + itemNo, new Callback() {
            @Override
            public void onSuccess(Object obj) {
                onResultListener.onSuccess(obj);
            }

            @Override
            public void onFailed(Exception e) {
                getItemNo(itemNo, onResultListener);
//                onResultListener.onFails(e);
            }
        }, typeofT);
    }


    public void getSyncedItemNo(CtmSyncedQuery query, OnResultListener onResultListener) {
        ThreadManager.getInstance().execute(() -> {
//            List<SyncedItemDto> itemNoStore = mDao.getItemNoStore(query.getKeyWord(), query.getStartDate(), query.getEndDate()
//                    , query.getOrderBySyncedAt(), query.getOrderBySyncStatus(), Utils.getOffset(query.getPageIndex()), Utils.getLimit());
            List<SyncedItemDto> itemNoStore = mDao.getItemNoStore(mDao.getItemNoStoreSql(query, Utils.getOffset(query.getPageIndex()), Utils.getLimit()));
            int count = mDao.getItemNoCount(query.getKeyWord(), query.getStartDate(), query.getEndDate());
            int totalPages = (count / 10) + 1;
            ApiResultData<Object> result = new ApiResultData<>();
            result.setStatus(ApiStatus.SUCCESS);
            result.setMessage(DbMsg.QUERY_SUCCESS);
            PagedList<SyncedItemDto> pagedList = new PagedList<>();
            pagedList.setPageIndex(query.getPageIndex());
            pagedList.setTotalPages(totalPages);
            pagedList.setList(itemNoStore);
            pagedList.setTotalCount(count);
            result.setData(pagedList);
            OKHttpUtil.getInstance().post(() -> onResultListener.onSuccess(result));
        });
    }

    public int insertItemNoStores(List<ItemNoStore> itemNoStores, TableVersionRepository tableVersionRep) {
        if (itemNoStores == null || itemNoStores.isEmpty()) {
            return 0;
        }
        int ver = 0;
        for (int i = 0; i < itemNoStores.size(); i++) {
            itemNoStores.get(i).setSyncedAt(new Date());
            itemNoStores.get(i).setSyncedStatus(ApiStatus.DOWNLOAD_SUCCESS);
            int synVer = itemNoStores.get(i).getSynVer();
            if (ver < synVer) {
                ver = synVer;
            }
        }
        mDao.inserts(itemNoStores);
        updateTableVersion(tableVersionRep, ver);
        return itemNoStores.size();
    }
}
